/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.sync.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.box.entity.Box;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.security.Md5;
import com.pitop.customer.entity.Customer;
import com.pitop.order.entity.Order;
import com.pitop.sync.service.SyncFactorySVImpl;
import com.pitop.test.entity.TestRecord;
import com.pitop.test.entity.TestUnit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * 需要同步的数据 控制器
 *
 * @author pi-top
 */
@RestController
@RequestMapping("/syncFactoryData")
@Slf4j
@Api(value = "需要同步的数据控制器", description = "需要同步的数据控制器")
public class SyncFactoryDataCtrl {

    @Autowired
    private SyncFactorySVImpl syncFactorySV;

    /**
     * 从工厂向云端同步数据
     * 1.验证数据的完整性
     * 2.数据转化
     * 3.数据备份
     *
     * @param md5 数据签名
     * @return BeanRet
     */
    @ApiOperation(value = "syncDataFromDown 从工厂向云端同步数据", notes = "syncDataFromDown 从工厂向云端同步数据")
    @RequestMapping(value = "/syncDataFromDown", method = RequestMethod.POST)
    @ResponseBody
    public BeanRet syncDataFromDown(@RequestBody String json, String md5) throws Exception {
        //1.验证数据的完整性
        if (StringUtils.isBlank(json) || StringUtils.isBlank(md5)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Map<String, Object> paraMap = JSON.parseObject(json);
        json = String.valueOf(paraMap.get("json"));
        String md5Str = Md5.md5(json);
        if (!md5Str.equals(md5)) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
        }

        String unicodeStr = new String(Base64Utils.decodeFromString(json), "UTF-8");
        json = StringTools.Unicode2Cn(unicodeStr);

        //2.数据转化
        List<TestUnit> testUnitList = JSON.parseArray(json, TestUnit.class);
        if (testUnitList == null || testUnitList.isEmpty()) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
        }

        //3.数据备份
        boolean flag = syncFactorySV.upwardsSyncTestUnit(testUnitList);
        if (flag) {
            return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
        } else {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
        }
    }


    /**
     * 从工厂向云端同步测试件对应的测试记录数
     * 1.验证数据的完整性
     * 2.数据转化
     * 3.数据备份
     *
     * @param md5 数据签名
     * @return BeanRet
     */
    @ApiOperation(value = "upwardsSyncTestRecord 从工厂向云端同步测试件对应的测试记录数据", notes = "upwardsSyncTestRecord 从工厂向云端同步测试件对应的测试记录数据")
    @RequestMapping(value = "/upwardsSyncTestRecord", method = RequestMethod.POST)
    @ResponseBody
    public BeanRet upwardsSyncTestRecord(@RequestBody String json, String md5) {
        //1.验证数据的完整性
        if (StringUtils.isBlank(json) || StringUtils.isBlank(md5)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Map<String, Object> paraMap = JSON.parseObject(json);
        json = paraMap.get("json").toString();
        String md5Str = Md5.md5(json);
        if (!md5Str.equals(md5)) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String unicodeStr;
        try {
            unicodeStr = new String(Base64Utils.decodeFromString(json), "UTF-8");
            json = StringTools.Unicode2Cn(unicodeStr);
            //2.数据转化
            List<TestRecord> testRecordList = JSON.parseArray(json, TestRecord.class);
            if (testRecordList == null || testRecordList.isEmpty()) {
                return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
            }
            //3.同步数据
            boolean flag = syncFactorySV.upwardsSyncTestRecord(testRecordList);
            if (flag) {
                return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
            } else {
                return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
        }
    }


    /**
     * 从工厂向云端同步箱库存数据
     * 1.验证数据的完整性
     * 2.数据转化
     * 3.数据备份
     *
     * @param md5 数据签名
     * @return BeanRet
     */
    @ApiOperation(value = "upwardsSyncBox 从工厂向云端同步箱库存数据", notes = "upwardsSyncBox 从工厂向云端同步箱库存数据")
    @RequestMapping(value = "/upwardsSyncBox", method = RequestMethod.POST)
    @ResponseBody
    public BeanRet upwardsSyncBox(@RequestBody String json, String md5) {
        //1.验证数据的完整性
        if (StringUtils.isBlank(json) || StringUtils.isBlank(md5)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Map<String, Object> paraMap = JSON.parseObject(json);
        json = paraMap.get("json").toString();
        String md5Str = Md5.md5(json);
        if (!md5Str.equals(md5)) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String unicodeStr;
        try {
            unicodeStr = new String(Base64Utils.decodeFromString(json), "UTF-8");
            json = StringTools.Unicode2Cn(unicodeStr);
            //2.数据转化
            List<Box> boxList = JSON.parseArray(json, Box.class);
            if (boxList == null || boxList.isEmpty()) {
                return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
            }
            //3.同步数据
            boolean flag = syncFactorySV.upwardsSyncBox(boxList);
            if (flag) {
                return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
            } else {
                return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
        }
    }

    /**
     * 从工厂向云端同步用户信息数据
     * 1.验证数据的完整性
     * 2.数据转化
     * 3.数据备份
     *
     * @param md5 数据签名
     * @return BeanRet
     */
    @ApiOperation(value = "upwardsSyncBox 从工厂向云端同步用户信息数据", notes = "upwardsSyncBox 从工厂向云端同步用户信息数据")
    @RequestMapping(value = "/upwardsSyncCustomer", method = RequestMethod.POST)
    @ResponseBody
    public BeanRet upwardsSyncCustomer(@RequestBody String json, String md5) {
        //1.验证数据的完整性
        if (StringUtils.isBlank(json) || StringUtils.isBlank(md5)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Map<String, Object> paraMap = JSON.parseObject(json);
        json = paraMap.get("json").toString();
        String md5Str = Md5.md5(json);
        if (!md5Str.equals(md5)) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String unicodeStr;
        try {
            unicodeStr = new String(Base64Utils.decodeFromString(json), "UTF-8");
            json = StringTools.Unicode2Cn(unicodeStr);
            //2.数据转化
            List<Customer> customerList = JSON.parseArray(json, Customer.class);
            if (customerList == null || customerList.isEmpty()) {
                return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
            }
            //3.同步数据
            boolean flag = syncFactorySV.upwardsSyncCustomer(customerList);
            if (flag) {
                return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
            } else {
                return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
        }
    }

    /**
     * 从工厂向云端同步订单数据
     * 1.验证数据的完整性
     * 2.数据转化
     * 3.数据备份
     *
     * @param md5 数据签名
     * @return BeanRet
     */
    @ApiOperation(value = "upwardsSyncOrder 从工厂向云端同步订单数据", notes = "upwardsSyncOrder 从工厂向云端同步订单数据")
    @RequestMapping(value = "/upwardsSyncOrder", method = RequestMethod.POST)
    @ResponseBody
    public BeanRet upwardsSyncOrder(@RequestBody String json, String md5) {
        //1.验证数据的完整性
        if (StringUtils.isBlank(json) || StringUtils.isBlank(md5)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Map<String, Object> paraMap = JSON.parseObject(json);
        json = paraMap.get("json").toString();
        String md5Str = Md5.md5(json);
        if (!md5Str.equals(md5)) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String unicodeStr;
        try {
            unicodeStr = new String(Base64Utils.decodeFromString(json), "UTF-8");
            json = StringTools.Unicode2Cn(unicodeStr);
            //2.数据转化
            List<Order> orderList = JSON.parseArray(json, Order.class);
            if (orderList == null || orderList.isEmpty()) {
                return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
            }
            //3.同步数据
            boolean flag = syncFactorySV.upwardsSyncOrder(orderList);
            if (flag) {
                return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
            } else {
                return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Server_Error);
        }
    }

}
